package com.glsc.ngateway.opmanage.controller.ledger;

import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuthStaff;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuthTemplate;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.service.ledger.LedgerAuthStaffService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.data.domain.Page;
import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuth;
import com.glsc.ngateway.opmanage.dto.LedgerAuthDto;
import com.glsc.ngateway.opmanage.service.ledger.LedgerAuthService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 台账权限配置
 */
@RestController
@RequestMapping("/api/ledger/auth")
@Slf4j
public class LedgerAuthController {

    @Resource
    private UserCache userCache;

    @Resource
    private LedgerAuthService ledgerAuthService;

    @Resource
    private LedgerAuthStaffService ledgerAuthStaffService;

    @GetMapping("/listByUserId")
    public PlatformResponse<List<LedgerAuth>> listByUserId(HttpServletRequest request) {
        User user = userCache.get(request);
        List<LedgerAuthStaff> ledgerAuthStaffs = ledgerAuthStaffService.listByUserOaAccount(user.getUsername());
        if (CollectionUtils.isEmpty(ledgerAuthStaffs)) {
            return PlatformResponse.successData(Lists.newArrayList());
        }
        List<LedgerAuth> ledgerAuths = ledgerAuthService.listByIds(ledgerAuthStaffs.stream().map(LedgerAuthStaff::getLedgerAuthId).collect(Collectors.toList()));
        return PlatformResponse.successData(ledgerAuths);
    }


    /**
     * 分页查询
     *
     * @param obj
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<LedgerAuth>> findClientPage(@RequestBody LedgerAuthDto obj) {

        Map<String, Object> param = new HashMap<>();

        if (StringUtils.isNotBlank(obj.getName())) {
            param.put("LIKE_name", obj.getName());
        }

        Page<LedgerAuth> page = ledgerAuthService.page(PageUtil.initPageNo(obj.getPageNo()), PageUtil.initPageSize(obj.getPageSize()), param);
        return PlatformResponse.successData(page);
    }

    /**
     * 根据di查询
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<LedgerAuth> findById(@ApiParam(value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(ledgerAuthService.findById(id));
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<LedgerAuth> save(@RequestBody LedgerAuth obj, HttpServletRequest request) {
        User user = userCache.get(request);
        obj.setUserName(user.getName());
        obj.setUserId(user.getId());
        LedgerAuth save = ledgerAuthService.save(obj);
        return PlatformResponse.successData(save);
    }

    /**
     * 删除接口
     *
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody LedgerAuth obj) {
        ledgerAuthService.deleteById(obj.getId());
        return PlatformResponse.success();
    }


}
